package com.software.levelupserver.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadUtil {
    public static final String ALI_DOMAIN = "https://level--up.oss-cn-beijing.aliyuncs.com/";
    public static String uploadImage(MultipartFile multipart) throws IOException {
        String originalFileName = multipart.getOriginalFilename();
        String suffix = "." + originalFileName.split("\\.")[1];
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName = uuid + suffix;

        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5tJgREYAssc2w6gyQSFp";
        String accessKeySecret = "GBlTm2ipIhpfSOr6liAkBhPro0iwwV";
        OSS ossClient = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        ossClient.putObject(
                "level--up",
                fileName,
                multipart.getInputStream()
        );
        ossClient.shutdown();
        return ALI_DOMAIN + fileName;
    }
}
